package com.bigdata.service.mysql.impl;

import com.bigdata.common.em.CommonEnum;
import com.bigdata.model.Result;
import com.bigdata.model.SearchModel;
import com.bigdata.model.User;
import com.bigdata.model.ZTreeBean;
import com.bigdata.mysql.dao.JkfwMenuMapper;
import com.bigdata.mysql.entity.JkfwMenu;
import com.bigdata.mysql.entity.JkfwMenuExample;
import com.bigdata.mysql.vo.VoJkfwMenu;
import com.bigdata.service.mysql.JkfwMenuPinyinService;
import com.bigdata.service.mysql.JkfwMenuService;
import com.bigdata.service.mysql.JkfwRoleMenuService;
import com.bigdata.service.mysql.base.impl.BaseService;
import com.bigdata.utils.shiro.ShiroUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;


/**
 * JkfwMenu的服务接口的实现类
 *
 * @author
 */
@Service
public class JkfwMenuServiceImpl extends BaseService<JkfwMenuMapper, JkfwMenu, JkfwMenuExample> implements JkfwMenuService {
    private final Logger log = LogManager.getLogger(this.getClass());

    @Autowired
    JkfwRoleMenuService jkfwRoleMenuService;
    @Autowired
    JkfwMenuPinyinService jkfwMenuPinyinService;

    @Override
    public Result queryPage(SearchModel searchModel) {
        JkfwMenuExample example = new JkfwMenuExample();
        JkfwMenuExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(searchModel.getS_name())) {
            criteria.andNameLike("%" + searchModel.getS_name() + "%");
        }
        if (StringUtils.isNoneBlank(searchModel.getSd1())) {
            criteria.andParentIdEqualTo(searchModel.getSd1());
        }
        List<JkfwMenu> list = dao.selectByExample(example);
        return Result.pageOk(list);
    }

    /**
     * @Author lly
     * @param: searchModel
     * @Date 2019/8/23 0023
     * @return: com.bigdata.model.Result
     * @Description 查询所有请求
     */
    @Override
    public Result querySearch(SearchModel searchModel) {
        if (ShiroUtils.isUserManager()) {
            searchModel.setAdmin(true);
        }
        List<JkfwMenu> list = dao.querySearch(searchModel);
        return Result.pageOk(list);
    }

    @Override
    public Result queryGdTree(SearchModel searchModel) {
        JkfwMenuExample example = new JkfwMenuExample();
        JkfwMenuExample.Criteria criteria = example.createCriteria();
        if (StringUtils.isNoneBlank(searchModel.getS_name())) {
            criteria.andNameLike("%" + searchModel.getS_name() + "%");
        }
        if (StringUtils.isNoneBlank(searchModel.getNodeid())) {
            criteria.andParentIdEqualTo(searchModel.getNodeid());
        } else {
            criteria.andParentIdEqualTo("0");
        }
        List<JkfwMenu> list = dao.selectByExample(example);
        List<VoJkfwMenu> voJkfwMenus = new ArrayList<VoJkfwMenu>();
        for (JkfwMenu jkfwMenu : list) {
            VoJkfwMenu vo = new VoJkfwMenu(jkfwMenu);
            if (StringUtils.isNoneBlank(searchModel.getN_level())) {
                vo.setLevel((Integer.parseInt(searchModel.getN_level()) + 1));
            }
            voJkfwMenus.add(vo);

        }
        return Result.pageOk(voJkfwMenus);
    }

    @Override
    @Transactional(readOnly = false)
    public Result addMenu(JkfwMenu jkfwMenu) {
        jkfwMenu.setMenuId(UUID.randomUUID().toString().replace("-", ""));
        dao.insert(jkfwMenu);
        jkfwMenuPinyinService.addPinyin(jkfwMenu);
        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result updateMenu(JkfwMenu jkfwMenu) {
        dao.updateByPrimaryKeySelective(jkfwMenu);
        jkfwMenuPinyinService.delMenuid(jkfwMenu.getMenuId());
        jkfwMenuPinyinService.addPinyin(jkfwMenu);
        return Result.ok();
    }

    @Override
    @Transactional(readOnly = false)
    public Result delMenu(String id) {
        JkfwMenuExample example = new JkfwMenuExample();
        example.createCriteria().andParentIdEqualTo(id);
        List<JkfwMenu> menus = dao.selectByExample(example);
        if (menus.size() > 0) {
            return Result.fail("请先删除子节点数据");
        }
        dao.deleteByPrimaryKey(id);
        jkfwRoleMenuService.delMenuid(id);
        jkfwMenuPinyinService.delMenuid(id);
        return Result.ok();
    }

    /**
     * @Author lly
     * @param: request
     * @param: pid
     * @param: type
     * @Date 2019/10/28 0028
     * @return: java.util.List<com.bigdata.model.ZTreeBean>
     * @Description
     */
    @Override
    public List<ZTreeBean> getTreeBeans(HttpServletRequest request, String pid, String type) {
        List<ZTreeBean> treeBeans = new ArrayList<ZTreeBean>();
        JkfwMenuExample example = new JkfwMenuExample();
        example.setOrderByClause("order_num desc");
        if (StringUtils.isNoneBlank(pid)) {
            example.createCriteria().andParentIdEqualTo(pid);
        }
        List<JkfwMenu> menus = dao.selectByExample(example);
        cgZtree(menus, pid, type, treeBeans);
        return treeBeans;
    }

    /**
     * @Author lly
     * @Date 2019/10/28 0028
     * @return: java.util.List<com.bigdata.model.ZTreeBean>
     * @Description 用户模块树
     */
    @Override
    public List<ZTreeBean> userHomeTreeBeans(String pid, HttpServletRequest request, String type) {
        List<ZTreeBean> treeBeans = new ArrayList<ZTreeBean>();
        List<JkfwMenu> menus = null;
        if (ShiroUtils.isUserManager()) {
            JkfwMenuExample example = new JkfwMenuExample();
            example.setOrderByClause(" order_num desc ");
            menus = dao.selectByExample(example);
        } else {
            SearchModel searchModel = new SearchModel();
            User user = ShiroUtils.getSysUser();
            searchModel.setSd1(user.getUserId());
            menus = dao.userTreeMenu(searchModel);
        }
        cgZtree(menus, pid, type, treeBeans);
        return treeBeans;
    }

    private void cgZtree(List<JkfwMenu> menus, String pid, String type, List<ZTreeBean> treeBeans) {
        Iterator<JkfwMenu> itr = menus.iterator();
        ZTreeBean tree = null;
        while (itr.hasNext()) {
            JkfwMenu xk = itr.next();
            if (!StringUtils.isNotBlank(pid)) {
                pid = "0";
            }
            if (xk.getParentId().equals(pid)) {
                if (StringUtils.isNotBlank(type) && type.equals("menu")) {
                    if (xk.getType().intValue() == CommonEnum.MenuEm.qx.getIdx()) {
                        continue;
                    }
                }
                tree = new ZTreeBean();
                tree.setId(xk.getMenuId());
                tree.setName(xk.getName());
                tree.setTags(xk);
                tree.setStrurl(xk.getUrl());
                tree.setMenus(xk.getType().toString());
                tree.setChildren(getTree(xk, menus, type));
                if (StringUtils.isNotBlank(type) && (type.equals("fuc") || type.equals("menu"))) {
                    tree.setIsParent(true);
                }
                if (tree.getChildren().size() > 0) {
                    tree.setIsParent(true);
                }
                treeBeans.add(tree);
            }
        }
    }

    private List<ZTreeBean> getTree(JkfwMenu xk, List<JkfwMenu> list, String type) {
        List<ZTreeBean> treeBeans = new ArrayList<ZTreeBean>();
        Iterator<JkfwMenu> itr = list.iterator();
        ZTreeBean treeBean = null;
        while (itr.hasNext()) {
            JkfwMenu xtk = itr.next();
            if (xtk.getParentId().equals(xk.getMenuId())) {
                if (StringUtils.isNotBlank(type) && type.equals("menu")) {
                    if (xtk.getType().intValue() == CommonEnum.MenuEm.qx.getIdx()) {
                        continue;
                    }
                }
                treeBean = new ZTreeBean();
                treeBean.setId(xtk.getMenuId());
                treeBean.setName(xtk.getName());
                treeBean.setTags(xtk);
                treeBean.setStrurl(xtk.getUrl());
                treeBean.setMenus(xtk.getType().toString());
                treeBean.setChildren(getTree(xtk, list, type));
                if (StringUtils.isNotBlank(type) && (type.equals("fuc") || type.equals("menu"))) {
                    treeBean.setIsParent(true);
                }
                if (treeBean.getChildren().size() > 0) {
                    treeBean.setIsParent(true);
                }
                treeBeans.add(treeBean);
            }
        }
        return treeBeans;
    }
}